package com.eorchis.module.user.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.eorchis.core.basedao.dao.IDaoSupport;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.service.impl.AbstractBaseService;
import com.eorchis.core.ui.commond.ICommond;
import com.eorchis.module.classuser.dao.IClassUserDao;
import com.eorchis.module.classuser.domain.ClassUser;
import com.eorchis.module.user.dao.IUserUpdateDao;
import com.eorchis.module.user.domain.ClassUserChangeLog;
import com.eorchis.module.user.service.IUserUpdateService;
import com.eorchis.module.user.ui.commond.UserInfoValidCommond;
import com.eorchis.utils.utils.PropertyUtil;

@Service(value = "com.eorchis.module.user.service.impl.UserUpdateServiceImpl")
public class UserUpdateServiceImpl extends AbstractBaseService implements IUserUpdateService {

	@Resource(name = "com.eorchis.module.user.dao.impl.UserUpdateDaoimpl")
	private IUserUpdateDao userUpdateDao;
	@Resource(name = "com.eorchis.module.classuser.dao.impl.ClassUserDaoImpl")
	private IClassUserDao classUserDao;

	@Override
	public IDaoSupport getDaoSupport() {
		return userUpdateDao;
	}

	@Override
	public <C extends ICommond> C toCommond(IBaseEntity paramIBaseEntity) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("deprecation")
	@Override
	public String saveChangeinfoLog(UserInfoValidCommond userValidCommond) throws Exception {
		ClassUserChangeLog classUserChangeLog = new ClassUserChangeLog();
		// 查询修改前用户信息
		List<UserInfoValidCommond> classUserList = userUpdateDao.queryChengeBeforeUserInof(userValidCommond);
		UserInfoValidCommond classUser = classUserList.get(0);
		// 拼修改信息
		StringBuffer changeInfo = new StringBuffer();
		changeInfo.append("姓名：原： " + classUser.getUserName() + "===改为==>>" + userValidCommond.getUserName());
		changeInfo.append("；	性别：原： " + classUser.getSexCode() + "===改为==>>" + userValidCommond.getSexCode());
		changeInfo.append("；	出生年月：原： " + userValidCommond.getBirthday() 	+ "===改为==>>" + userValidCommond.getBirthdayYear() + userValidCommond.getBirthdayMouth());
		changeInfo.append("；	名族：原： " + classUser.getFolkCode() + "===改为==>>" + userValidCommond.getFolkCode());
		changeInfo.append("；	名族：原： " + classUser.getMobileTelephone() + "===改为==>>" + userValidCommond.getMobileTelephone());
		// 获取原单位
		Object o = classUserDao.getUserDeptNameByUserId(userValidCommond);
		if(!PropertyUtil.objectNotEmpty(o)){
			o="承办单位（培训系统）";
		}
		changeInfo.append("；	单位：原： " + o.toString() + "===改为==>>" + userValidCommond.getDeptName());
		changeInfo.append("；	职级：原： " + classUser.getRank() + "===改为==>>" + userValidCommond.getRank());
		changeInfo.append("；	职务：原： " + classUser.getDutyRemark() + "===改为==>>" + userValidCommond.getDutyRemark());
		changeInfo.append("；	备注：原： " + classUser.getRemark() + "===改为==>>" + userValidCommond.getRemark());
		// 设置用户ID
		classUserChangeLog.setUserID(userValidCommond.getUserId());
		// 设置修改信息
		classUserChangeLog.setChangeInfoLog(changeInfo.toString());
		// 设置修改时间
		classUserChangeLog.setChangeDate(new Date());
		//验证表中是否已经有该用户数据
		List<ClassUserChangeLog> changeLogList = userUpdateDao.queryChangeLogByUserId(userValidCommond);
		// 保存这条修改信息
		if(PropertyUtil.objectNotEmpty(changeLogList)){
			classUserChangeLog.setChangelogID(changeLogList.get(0).getChangelogID());
			userUpdateDao.update(classUserChangeLog);
		}else{
			userUpdateDao.save(classUserChangeLog);
		}
		return "";
	}

}
